<?php
/**
* Copyright (c) 2002-2007 BioWare Corp.
* http://www.bigdaddymonitor.org
*
* This file is part of the BigDaddy Monitoring
* program. It is free software distributed under
* the terms of the BSD License. It is ditributed
* in the hopes that it will be useful, but
* WITHOUT ANY WARRANTY; without event the implied
* warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.
*
* The BSD License is available from:
* http://www.opensource.org/licenses/bsd-license.php
**/
/**
 * Class::BigDaddy::BigDaddyData
 * Main classes for getting BigDaddy Data for display from the BigDaddy Database
 * @author Duleepa Wijayawardhana <dups@bioware.com>
 * @version 30/05/2006 11:38:06
 */
class BigDaddyData extends BigDaddy
{
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * array GetFailingServerModules()
     * returns an array of server modules which are failing
     *
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:08:06
     */
    public function GetFailingServerModules()
    {
       $sql="SELECT sm.ID as serverModuleID, sm.moduleID, sm.serverID, sm.iniID, sm.active, sm.parameters,
            sm.mon_currentlevel, sm.mon_lastreport, m.name as module_name, s.hostname
            FROM server_module sm
            LEFT JOIN module m ON m.ID=sm.moduleID
            LEFT JOIN server s ON s.ID=sm.serverID
            WHERE sm.mon_currentlevel>0
                AND sm.active=1
                AND s.active=1";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetFailingServers()
     * returns an array of servers which are failing
     *
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:11:08
     */
    public function GetFailingServers()
    {
        
        $sql="SELECT s.ID as serverID, s.hostname, s.mon_currentlevel,
        s.last_internalcheck, s.last_externalcheck, s.active, s.delayed_until
        FROM server s
        WHERE s.mon_currentlevel>0
            AND s.active=1
            ORDER BY s.mon_currentlevel DESC, s.hostname ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetFailingCrons()
     * returns an array of crons which are failing
     *
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:23:04
     */
    public function GetFailingCrons()
    {
        $sql="SELECT c.ID as cronID, c.filename, c.last_ranend, c.last_ranstart,
        c.mon_currentlevel, c.active, c.delayed_until, c.ignoreMonitor, s.hostname
        FROM cronjob c
        LEFT JOIN server s ON s.ID=c.serverID
        WHERE c.mon_currentlevel>0
            AND c.active=1
        ORDER BY c.mon_currentlevel DESC, c.filename ASC";
        
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetFailingServerGroups()
     * returns an array of server groups
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 02/06/2006 12:30:09
     */
    public function GetFailingServerGroups()
    {
        $sql="SELECT sg.ID as serverGroupID, sg.groupname,
                sg.mon_currentlevel, sg.active, sg.delayed_until
            FROM server_group sg
            WHERE sg.active=1
                AND sg.mon_currentlevel > 0
            ORDER BY sg.mon_currentlevel DESC,sg.groupname ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
            
    }
    /**
     * array GetMonitoringLastReportInfo()
     * returns an array of information about the monitoring last reported
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:35:52
     */
    public function GetMonitoringLastReportInfo()
    {
        $sql="SELECT ID, report, action, processID FROM report";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetServerIDsWithFailedModules()
     * returns an array of serverIDs with failed modules
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:56:43
     */
    public function GetServerIDsWithFailedModules()
    {
        $sql="SELECT distinct(serverID) FROM server_module WHERE active=1 AND mon_currentlevel>0";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            foreach ($aResult as $nIndex => $aRow)
            {
                $aReturn[] = $aRow["serverID"];
            }
            return $aReturn;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetServers($aCriteria)
     * returns an array of servers in BigDaddy based on various criteria
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 17:10:13
     */
    public function GetServers($aCriteria)
    {
        switch ($aCriteria["active"])
        {
            case -1:
                $sActive = "";
                break;
            case 0:
            case 1:
            case 2:
                $sActive = " WHERE s.active = ".$aCriteria["active"];
                break;
            default:
                $sActive = "";
                break;
                
        }
        
        if ($aCriteria["max_results"]>0)
        {
            $sLimit = " LIMIT ".$aCriteria["start_number"].", ".$aCriteria["max_results"];
        }
        
        if ($aCriteria["catID"]>0)
        {
            $sql="SELECT SQL_CALC_FOUND_ROWS distinct(m.serverID), s.hostname, s.mon_currentlevel, s.last_internalcheck,
                s.last_externalcheck, s.active, s.delayed_until
            FROM server_category_map m
            LEFT JOIN server s ON s.ID=m.serverID
            WHERE m.categoryID='".$aCriteria["catID"]."'";
        }
        else
        {
            $sql="SELECT SQL_CALC_FOUND_ROWS s.ID as serverID, s.hostname, s.mon_currentlevel, s.last_internalcheck,
                s.last_externalcheck, s.active, s.delayed_until
            FROM server s
            $sActive";
        }
        
        $sql.= " ORDER BY s.mon_currentlevel DESC,s.hostname ASC
            $sLimit";

        
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            
            $sql = "SELECT FOUND_ROWS() as TotalRows";
            $aTotal = db_fetch_result(db_query($sql));

            $aReturn["aResults"] = $aResult;
            $aReturn["nTotalRows"] = $aTotal["TotalRows"];
            
            return $aReturn;
        }
        else
        {
            return 0;
        }
        
        
    }
    /**
     * string GetCategoryNameByID($nID)
     * Returns a string name of the category
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 10:31:56
     */
    public function GetCategoryNameByID($nID)
    {
        $sql="SELECT name FROM server_category WHERE ID='".$nID."'";
        $aResult=db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult["name"];
        }
        
        return;
    }
    /**
     * array GetServerCategories()
     * Returns an array of information about server categories
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 10:36:43
     */
    public function GetServerCategories()
    {
        $sql="SELECT name, ID
            FROM server_category
            ORDER BY name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerInfoByID($nID)
     * Returns an array of information about a particular server
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 10:41:09
     */
    public function GetServerInfoByID($nID)
    {
        $sql="SELECT * FROM server WHERE ID='".$nID."'";
		$aResult=db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * bool IsServerModulesInstalledByServerID($nID)
     * Returns true if a server has server modules installed
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 10:47:48
     */
    public function IsServerModulesInstalledByServerID($nID)
    {
        $sql="SELECT COUNT(*) as count FROM server_module WHERE serverID=".$nID;
        $aResult=db_fetch_result(db_query($sql));
        
        if ($aResult["count"]>0)
        {
            return true;
        }
        
        return false;
    }
    /**
     * array GetServerModulesByServerID()
     * returns an array of server modules for a particular serverID
     *
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 30/05/2006 16:08:06
     */
    public function GetServerModulesByServerID($nID)
    {
       $sql="SELECT sm.ID as serverModuleID, sm.moduleID, sm.serverID, sm.iniID, sm.active, sm.parameters,
            sm.mon_currentlevel, sm.mon_lastreport, m.name as module_name, m.type
            FROM server_module sm
            LEFT JOIN module m ON m.ID=sm.moduleID
            WHERE sm.serverID=".$nID."
            ORDER BY m.name, sm.parameters, sm.ID ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetIniInfoByID($sID)
     * returns an array of information about an ini for aparticular ID
     *
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:00:27
     */
    public function GetIniInfoByID($sID)
    {
        $sql="SELECT ID, name
            FROM inifile
            WHERE ID='".$sID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetIniFileByID($sID)
     * returns a row of information of an IniFile By string ID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 10:18:13
     */
    public function GetIniFileByID($sID)
    {
        $sql="SELECT *
            FROM inifile
            WHERE ID='".$sID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerCategoriesByServerID($nID)
     * returns an array of categories for a particular serverID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:04:06
     */
    public function GetServerCategoriesByServerID($nID)
    {
        $sql="SELECT m.categoryID, c.name
        FROM server_category_map m
        LEFT JOIN server_category c ON c.ID=m.categoryID
        WHERE m.serverID='".$nID."'
        ORDER BY c.name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetIniFiles()
     * returns an array of inifiles
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:07:36
     */
    public function GetIniFiles()
    {
        $sql="SELECT name, ID FROM inifile ORDER BY name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetServerModuleInfoByModuleID($nID)
     * returns an array of info about a server module by ID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:17:59
     */
    public function GetServerModuleInfoByModuleID($nID)
    {
        $sql="SELECT sm.*, m.name as module_name
        FROM server_module sm
        LEFT JOIN module m ON m.ID=sm.moduleID
        WHERE sm.ID='".$nID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        
        return 0;
    }
    /**
     * array GetModules()
     * returns an array of modules
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:22:39
     */
    public function GetModules()
    {
        $sql="SELECT name, ID FROM module ORDER BY name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetModuleByID($nID)
     * returns info about a particular module
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 10:46:34
     */
    public function GetModuleByID($nID)
    {
        $sql="SELECT * FROM module WHERE ID = '".$nID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetAllModuleInfo()
     * returns an array of modules (with all info and total count)
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 11:22:39
     */
    public function GetAllModuleInfo($nStart = NULL, $nMaxResults = NULL)
    {
        if ($nStart != NULL && $nMaxResults != NULL)
        {
            $sLimit = " LIMIT ".$nStart.",".$nMaxResults;
        }
        
        $sql="SELECT SQL_CALC_FOUND_ROWS m.*
            FROM module m
            ORDER BY m.name ASC
            $sLimit";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            $sql = "SELECT FOUND_ROWS() as TotalRows";
            $aTotal = db_fetch_result(db_query($sql));
            
            $aReturn["aResults"] = $aResult;
            $aReturn["nTotalRows"] = $aTotal["TotalRows"];
            
            return $aReturn;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerModules()
     * returns an array of servers and their modules
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 13:53:14
     */
    public function GetModulesByServerGroupID($nID)
    {
        $sql="SELECT sm.ID as serverModuleID, sm.moduleID, m.name as module_name, m.type, m.ID as moduleID, sm.serverID, s.hostname,
        sm.iniID, sm.active, sm.parameters, 
        sm.mon_currentlevel, sm.mon_lastreport
        FROM server_group_map sgm
        LEFT JOIN server_module sm ON sm.ID = sgm.server_moduleID
        LEFT JOIN module m ON m.ID=sm.moduleID
        LEFT JOIN server s ON s.ID = sm.serverID
        WHERE sgm.server_groupID = '".$nID."'
        ORDER BY s.hostname, m.name, sm.parameters ASC";

        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerModules()
     * returns an array of servers and their modules
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 13:53:14
     */
    public function GetModulesByReportGroupID($nID)
    {
        $sql="SELECT sm.ID as serverModuleID, sm.moduleID, m.name as module_name, m.type, m.ID as moduleID, sm.serverID, s.hostname,
        sm.iniID, sm.active, sm.parameters, 
        sm.mon_currentlevel, sm.mon_lastreport
        FROM report_group_map sgm
        LEFT JOIN server_module sm ON sm.ID = sgm.server_moduleID
        LEFT JOIN module m ON m.ID=sm.moduleID
        LEFT JOIN server s ON s.ID = sm.serverID
        WHERE sgm.report_groupID = '".$nID."'
        ORDER BY s.hostname, m.name, sm.parameters ASC";

        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerModules()
     * returns an array of servers and their modules
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 13:53:14
     */
    public function GetServerModules()
    {
        $sql="SELECT sm.ID as serverModuleID, sm.moduleID, m.name, sm.serverID, sm.parameters
        FROM server_module sm
        LEFT JOIN module m ON m.ID=sm.moduleID
        ORDER BY sm.serverID ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetCronCategoryInfoByID($nID)
     * returns an array of info for a cron category by ID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 15:58:35
     */
    public function GetCronCategoryInfoByID($nID)
    {
        $sql="SELECT name FROM cron_category WHERE ID='".$nID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    /**
     * array GetCronCategories()
     * returns an array of cron categories
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:00:32
     */
    public function GetCronCategories()
    {
        $sql="SELECT name, ID FROM cron_category ORDER BY name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetCrons($aCriteria)
     * returns an array of crons in BigDaddy based on various criteria
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:08:50
     */
    public function GetCrons($aCriteria)
    {
        switch ($aCriteria["active"])
        {
            case -1:
                $sActive = "";
                break;
            case 0:
            case 1:
            case 2:
                $sActive = " WHERE c.active = ".$aCriteria["active"];
                break;
            default:
                $sActive = "";
                break;
                
        }
        
        if ($aCriteria["max_results"]>0)
        {
            $sLimit = " LIMIT ".$aCriteria["start_number"].", ".$aCriteria["max_results"];
        }
        
        if ($aCriteria["running"] && $aCriteria["active"])
        {
            $sActive.=" AND c.last_ranstart > c.last_ranend ";
        }
            
        
        if ($aCriteria["catID"]>0)
        {
            $sql="SELECT SQL_CALC_FOUND_ROWS distinct(m.cronID), c.ignoreMonitor, c.filename, c.last_ranend,
                c.last_ranstart, c.mon_currentlevel, c.active, c.delayed_until, s.hostname
                FROM cron_category_map m
                LEFT JOIN cronjob c ON c.ID=m.cronID
                LEFT JOIN server s ON s.ID=c.serverID
                WHERE m.categoryID='".$aCriteria["catID"]."'";
        }
        else
        {
            $sql="SELECT SQL_CALC_FOUND_ROWS c.ID as cronID, c.filename, c.ignoreMonitor, c.last_ranend,
                c.last_ranstart, c.mon_currentlevel, c.active, c.delayed_until, s.hostname
                FROM cronjob c
                LEFT JOIN server s ON s.ID=c.serverID
                $sActive";
        }
        
        $sql.= " ORDER BY c.mon_currentlevel DESC, c.filename ASC
            $sLimit";
        
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            
            $sql = "SELECT FOUND_ROWS() as TotalRows";
            $aTotal = db_fetch_result(db_query($sql));

            $aReturn["aResults"] = $aResult;
            $aReturn["nTotalRows"] = $aTotal["TotalRows"];
            
            return $aReturn;
        }
        else
        {
            return 0;
        }
        
        
    }
    /**
     * array GetCronCategoriesByCronID($nID)
     * returns an array of cron categories by cronID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:26:32
     */
    public function GetCronCategoriesByCronID($nID)
    {
        $sql = "SELECT m.categoryID, c.name
		FROM cron_category_map m
		LEFT JOIN cron_category c ON c.ID=m.categoryID
		WHERE m.cronID='".$nID."'
        ORDER BY c.name ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
        
    }
    
    /**
     * array GetCronInfoByID($nID)
     * Returns an array of information about a particular cron
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 31/05/2006 16:35:33
     */
    public function GetCronInfoByID($nID)
    {
        $sql="SELECT * FROM cronjob WHERE ID='".$nID."'";
		$aResult=db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerGroupByID($nID)
     * Returns an array of information about a particular server group
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 18:27:02
     */
    public function GetServerGroupByID($nID)
    {
        $sql="SELECT * FROM server_group WHERE ID='".$nID."'";
		$aResult=db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerGroupByID($nID)
     * Returns an array of information about a particular server group
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 18:27:02
     */
    public function GetReportGroupByID($nID)
    {
        $sql="SELECT * FROM report_group WHERE ID='".$nID."'";
		$aResult=db_fetch_result(db_query($sql));
        
        if (is_array($aResult))
        {
            return $aResult;
        }
        else
        {
            return 0;
        }
    }
    /**
     * array GetServerGroups($aCriteria)
     * returns an array of server groups in BigDaddy based on various criteria
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 18:00:07
     */
    public function GetServerGroups($aCriteria)
    {
        switch ($aCriteria["active"])
        {
            case -1:
                $sActive = "";
                break;
            case 0:
            case 1:
            case 2:
                $sActive = " WHERE sg.active = ".$aCriteria["active"];
                break;
            default:
                $sActive = "";
                break;
                
        }
        
        if ($aCriteria["max_results"]>0)
        {
            $sLimit = " LIMIT ".$aCriteria["start_number"].", ".$aCriteria["max_results"];
        }
        
        $sql="SELECT SQL_CALC_FOUND_ROWS sg.ID as serverGroupID, sg.groupname,
                sg.mon_currentlevel, sg.active, sg.delayed_until
            FROM server_group sg
            $sActive";
       
        $sql.= " ORDER BY sg.mon_currentlevel DESC,sg.groupname ASC
            $sLimit";

        
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            
            $sql = "SELECT FOUND_ROWS() as TotalRows";
            $aTotal = db_fetch_result(db_query($sql));

            $aReturn["aResults"] = $aResult;
            $aReturn["nTotalRows"] = $aTotal["TotalRows"];
            
            return $aReturn;
        }
        else
        {
            return 0;
        }
        
        
    }
    /**
     * array GetServerGroups($aCriteria)
     * returns an array of server groups in BigDaddy based on various criteria
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version 01/06/2006 18:00:07
     */
    public function GetReportGroups($aCriteria)
    {
        
        if ($aCriteria["max_results"]>0)
        {
            $sLimit = " LIMIT ".$aCriteria["start_number"].", ".$aCriteria["max_results"];
        }
        
        $sql="SELECT SQL_CALC_FOUND_ROWS sg.ID as serverGroupID, sg.groupname
            FROM report_group sg
            $sActive";
       
        $sql.= " ORDER BY sg.groupname ASC
            $sLimit";

        
        $aResult = db_fetch_array(db_query($sql));
        
        if (is_array($aResult))
        {
            
            $sql = "SELECT FOUND_ROWS() as TotalRows";
            $aTotal = db_fetch_result(db_query($sql));

            $aReturn["aResults"] = $aResult;
            $aReturn["nTotalRows"] = $aTotal["TotalRows"];
            
            return $aReturn;
        }
        else
        {
            return 0;
        }
        
        
    }
    /**
     * GetPhoneTags
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 10:29:07 2006
     */
    public function GetPhoneTags()
    {
        $sql = "SELECT ID, phoneNumber, notes
            FROM phoneTag
            ORDER BY ID ASC";
        $aResult = db_fetch_array(db_query($sql));
        
        return $aResult;
            
    }
    /**
     * GetPhoneTagsByID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 10:29:21 2006
     */
    public function GetPhoneTagsByID($sTagID)
    {
        $sql = "SELECT ID, phoneNumber, notes
            FROM phoneTag
            WHERE ID = '".$sTagID."'";
        $aResult = db_fetch_result(db_query($sql));
        
        return $aResult;
            
    }
    /**
     * GetIncidents
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetIncidents($nStart = NULL, $nLimit = NULL)
    {
        $sql = "SELECT ID, incidentStart, incidentEnd, title, postedBy, postedTime, description
        FROM incident
        ORDER BY postedTime DESC";
        
        if ($nStart > 0 && $nLimit > 0)
        {
            $sql.= "LIMIT $nStart,$nLimit";
        }
        
        return db_fetch_array(db_query($sql));
    }
    /**
     * GetIncidentByID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetIncidentByID($nIncidentID)
    {
        $sql = "SELECT *
            FROM incident
            WHERE ID = '".$nIncidentID."'";
        return db_fetch_result(db_query($sql));
    }
    /**
     * GetServersByIncidentID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetServersByIncidentID($nIncidentID)
    {
        $sql = "SELECT s.hostname, si.serverID
            FROM incidentServer si
            LEFT JOIN server s ON s.ID = si.serverID
            WHERE si.incidentID = '".$nIncidentID."'";
        return db_fetch_array(db_query($sql));
    }
    /**
     * GetIncidentByServerID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetIncidentsByServerID($nServerID)
    {
        $sql = "SELECT si.incidentID, i.title, i.incidentStart, i.incidentEnd, i.description
            FROM incidentServer si
            LEFT JOIN incident i ON i.ID = si.incidentID
            WHERE si.serverID = '".$nServerID."'";
        return db_fetch_array(db_query($sql));
    }
    /**
     * GetCommentsByIncidentID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetCommentsByIncidentID($nIncidentID)
    {
        $sql = "SELECT *
            FROM incidentComment
            WHERE incidentID = '".$nIncidentID."'
            ORDER BY postedTime DESC";
        return db_fetch_array(db_query($sql));
    }
    /**
     * GetCommentByID
     * @author Duleepa Wijayawardhana <dups@bioware.com>
     * @version Mon Oct 30 20:10:00 2006
     */
    public function GetCommentByID($nCommentID)
    {
        $sql = "SELECT *
            FROM incidentComment
            WHERE ID = '".$nCommentID."'";
        return db_fetch_result(db_query($sql));
    }
    /**
     * GetPlotKitData
     * @author Dups <dups@dups.ca>
     * @return array
     */
    public function GetPlotKitData($sType, $aInfo)
    {
	
		$serverInfo = $this->GetServerInfoByID($aInfo["serverID"]);
		$moduleInfo = $this->GetServerModuleInfoByModuleID($aInfo["serverModuleID"]);
		//$nEndTime = time();
		
		switch ($aInfo["type"])
		{
			case 1:
				$sTitle = "Last Hour";
				$sTableType = NULL;
				$nStartTime = time() - 3600;
				$sTime = "%H:%M";
				break;
			case 2:
				$sTitle = "Last 24 Hours";
				$sTableType = "ma15";
				//$sTableType = NULL;
				$nStartTime = time() - 86400;
				$sTime = "%H:%M";
				break;
			case 3:
				$sTitle = "Last 3 Days";
				$sTableType = "ma15";
				//$sTableType = NULL;
				$nStartTime = time() - 3 * 86400;
				$sTime = "%d%m/%H%M";
				break;
			case 4:
				$sTitle = "Last 7 Days";
				$sTableType = "ma60";
				$nStartTime = time() - 7 * 86400;
				$sTime = "%d%m/%H%M";
				break;
			case 5:
				$sTitle = "Last 30 Days";
				$sTableType = "ma60";
				$nStartTime = time() - 30 * 86400;
				$sTime = "%d%m/%H%M";
				break;
			case 6:
				$sTitle = "Last 180 Days (6 Months)";
				$sTableType = "ma180";
				$nStartTime = time() - 180 * 86400;
				$sTime = "%d%m/%H%M";
				break;
			case 7:
				$sTitle = "Last Year";
				$sTableType = "ma180";
				$nStartTime = time() - 365 * 86400;
				$sTime = "%d%m/%H%M";
				break;
			case 8:
				$nStartTime = $aInfo["nStartTime"];
				$nEndTime = $aInfo["nEndTime"];
				$sTitle = "From ".get_time_str($nStartTime, 1)." To ".get_time_str($nEndTime, 1);
				$sTableType = "nonbool";
				
				$sTime = "%H:%M";
				break;
		}
		
		$sParam = str_replace("[hostname]", "", $moduleInfo["parameters"]);
		$sParam = trim($sParam);
		
		if ($sParam != "")
		{
			$sParam = "(".$sParam.")";
		}
		
		$sTitle.=": ".$serverInfo["hostname"]." ".$moduleInfo["module_name"]." $sParam";
        $BigDaddyHistory = new BigDaddyHistory;
        
		$aResult = $BigDaddyHistory->GetNonBooleanHistoryByTime($aInfo["serverModuleID"], $nStartTime, $sTableType, $nEndTime);

		if (is_array($aResult))
		{
			$i=0;
            //[0,41],[1,24],[2,38],[3,32],[4,43],[5,33],[6,29]
            //[{v:0, label:"071105"},{v:1, label:"071106"},{v:2, label:"071107"},{v:3, label:"071108"},{v:4, label:"071109"},{v:5, label:"071110"},{v:6, label:"071111"}]
            $nMod = ceil(count($aResult)/5);
			foreach ($aResult as $aRow)
			{
                if ($i%$nMod == 0)
                {
                    $xdata[] = "{v:$i, label:\"".strftime($sTime, $aRow["mon_lastreport"])."\"}";
                }
                
                $ydata[] = "[$i, {$aRow["lastreport"]}]";
				$i++;
			}
			
			//var_dump($xdata);
		
			$aReturn["dataset"] = implode(",", $ydata);
            $aReturn["xTicks"] = implode(",", $xdata);
            $aReturn["title"] = $sTitle;
            
            return($aReturn);
	
		}
    }

}
?>
